package com.joesem.sale.client.base

import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.joesem.sale.client.logic.login.LoginActivity

interface UiImpl {

    fun observerIntent(intent: Any?)

    fun showLoading()

    fun dismissLoading()


}

inline fun <reified VM : BaseViewModel> ComponentActivity.createViewModel(): Lazy<VM> {
    return viewModels<VM>().apply {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                if (this@createViewModel is UiImpl) {
                    this@apply.value.apply {
                        loadingStateData.observe(this@createViewModel) {
                            it?.let {
                                when (it) {
                                    BaseViewModel.LoadingState.LoadingShow -> showLoading()
                                    BaseViewModel.LoadingState.LoadingDismiss -> dismissLoading()
                                }
                            }
                        }
                    }.apply {
                        loginStateData.observe(this@createViewModel) {
                            it?.let {
                                when (it) {
                                    BaseViewModel.LoginError -> {
                                        launch<LoginActivity>()
                                    }
                                }
                            }
                        }
                    }.apply {
                        uiStateData.observe(this@createViewModel) {
                            it?.let {
                                observerIntent(it)
                            }
                        }
                    }
                }
            }
        })
    }
}


inline fun <reified VM : BaseViewModel> Fragment.createViewModel(): Lazy<VM> {
    return viewModels<VM>().apply {
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                if (this@createViewModel is UiImpl) {
                    this@apply.value.apply {
                        loadingStateData.observe(this@createViewModel) {
                            it?.let {
                                when (it) {
                                    BaseViewModel.LoadingState.LoadingShow -> showLoading()
                                    BaseViewModel.LoadingState.LoadingDismiss -> dismissLoading()
                                }
                            }
                        }
                    }.apply {
                        loginStateData.observe(this@createViewModel) {
                            it?.let {
                                when (it) {
                                    BaseViewModel.LoginError -> {
                                        ActivityUtils.finishAllActivities()
                                        launch<LoginActivity>()
                                    }
                                }
                            }
                        }
                    }.apply {
                        uiStateData.observe(this@createViewModel) {
                            it?.let {
                                observerIntent(it)
                            }
                        }
                    }
                }
            }
        })
    }
}
